<?php
    include('header.php');
    
    $db = db_connect();
    
    if(isset($_GET['type']) && isset($_GET['value'])){
	$type = $_GET['type'];
	$value = $_GET['value'];
	$select_title = '';
	
	if($type=='size'){ // By Size
	    switch($value){
		case 2:
		    $sizeRange = ' plan_size>200 AND plan_size<301 ';
		    $select_title = 'ขนาด 201-300 ตารางเมตร';
		    break;
		case 3:
		    $sizeRange = ' plan_size>300 AND plan_size<401 ';
		    $select_title = 'ขนาด 301-400 ตารางเมตร ';
		    break;
		case 4:
		    $sizeRange = ' plan_size>400 AND plan_size<501 ';
		    $select_title = 'ขนาด 401-500 ตารางเมตร ';
		    break;
		case 5:
		    $sizeRange = ' plan_size>500 ';
		    $select_title = 'ขนาด มากกว่า 500 ตารางเมตร ขึ้นไป ';
		    break;
		default :
		    $sizeRange = ' plan_size>99 AND plan_size<199 ';
		    $select_title = 'ขนาด 100-200 ตารางเมตร ';
		    break;
	    }
	    $sql = "SELECT * FROM house_plan WHERE $sizeRange ORDER BY plan_size";
	}else if($type=='style'){ // By Style
	    $select_title = 'CONTEMPORARY STYLE';
	    if($value==10){
		$sql = 'SELECT * FROM house_plan WHERE style_id=1 ORDER BY plan_name';
	    }else if($value==20){
		$sql = 'SELECT * FROM house_plan WHERE style_id=2 ORDER BY plan_name';
		$select_title = 'MODERN STYLE';
	    }else{
		$sql = 'SELECT * FROM house_plan WHERE style_id=1 ORDER BY plan_name';
	    }
	}else{ // By Default : Style (CONTEMPORARY STYLE)
	    $sql = 'SELECT * FROM house_plan WHERE style_id=1 ORDER BY plan_name';
	}
    }else{ // By Default : Style (CONTEMPORARY STYLE)
	echo '<META HTTP-EQUIV="Refresh" CONTENT="0;URL=index.php">';
	exit();
    }
    
    $result = mysql_query($sql);
    $house_list = '';
    $i = 1;
    
    while($row=mysql_fetch_assoc($result)){
	if($row['style_id']==1){
	    $house_plan_style = 'CONTEMPORARY STYLE';
	}else{
	    $house_plan_style = 'MODERN STYLE';
	}
	
	$plan_img_4 = '';
	    
	if($row['plan_img_4']!='' && file_exists('images/house/'.$row['style_id'].'/'.$row['plan_id'].'/'.$row['plan_img_4'])){
	    $plan_img_4 =<<<thumb_list
		<img src="images/house/{$row['style_id']}/{$row['plan_id']}/{$row['plan_img_4']}" alt="Plan 3" class="thumb_img" />
		<!--<p>Plan 4</p>-->
thumb_list;
	}
	
	$house_list .= <<<house_list
	    <div class="house_content">
		<div class="house_content_img">
		    <img src="images/house/{$row['style_id']}/{$row['plan_id']}/{$row['plan_img_1']}" alt="Perspective View" class="img_view_{$i}" />
		</div>
		<div class="house_content_detail">
		    <div class="house_content_detail_">
			<p class="plan_style">
			    {$house_plan_style}
			</p>
			<p class="plan_name">
			    แบบบ้าน : {$row['plan_name']}
			</p>
			<p class="plan_size">
			    พื้นที่ใช้สอย : {$row['plan_size']} ตร.ม.
			</p>
			<p class="plan_dimension">
			    ขนาดที่ดิน : {$row['plan_dimension_x']} ม. x {$row['plan_dimension_y']} ม.
			</p>
			<p class="plan_detail">
			    ส่วนประกอบของบ้าน : {$row['plan_detail']}
			</p>
		    </div>
		    <table class="thumb_img_tb" id="view_{$i}">
			<tr>
			    <td class="thumb_img_1">
				<img src="images/house/{$row['style_id']}/{$row['plan_id']}/{$row['plan_img_1']}" alt="Perspective View" class="thumb_img thumb_active" />
				<!--<p>Perspective</p>-->
			    </td>
			    <td class="thumb_img_2">
				<img src="images/house/{$row['style_id']}/{$row['plan_id']}/{$row['plan_img_2']}" alt="Plan 1" class="thumb_img" />
				<!--<p>Plan 1</p>-->
			    </td>
			    <td class="thumb_img_3">
				<img src="images/house/{$row['style_id']}/{$row['plan_id']}/{$row['plan_img_3']}" alt="Plan 2" class="thumb_img" />
				<!--<p>Plan 2</p>-->
			    </td>
			</tr>
			<tr>
			    <td class="thumb_img_1">
				{$plan_img_4}
			    </td>
			    <td class="thumb_img_2">
			    </td>
			    <td class="thumb_img_3">
			    </td>
			</tr>
		    </table>
		</div>
		<div class="clear">&nbsp;</div>
	    </div>
house_list;
	$i++;
    }
    
    db_close($db);
?>
    <div id="search_result">
        <h3>แบบบ้าน <?php echo $select_title; ?></h3>
	
	<?php echo $house_list; ?>
	
    </div>
    <script>
	$(document).ready(function(){
	    $('.thumb_img').click(function(){
		var thisEle = $(this);
		var planId = thisEle.parents('.thumb_img_tb').attr('id');
		$('#'+planId).find('img').removeClass('thumb_active');
		thisEle.addClass('thumb_active');
		$('.img_'+planId).attr('src',thisEle.attr('src'));
	    });
        });
    </script>
<?php
    include('footer.php');
?>